package org.ume.school.modules.acticles;

import org.ume.school.modules.comment.CommentRepository;
import org.ume.school.modules.comment.ThumbUpService;
import org.ume.school.modules.model.entity.Goods;
import org.ume.school.modules.model.entity.Comment;
import org.ume.school.modules.model.entity.Thumbup;
import org.ume.school.modules.utils.PageUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Django on 2017/7/22.
 */
@Service
public class ArticleService {

    @Resource
    private ArticleRepository articleRepository;

    @Resource
    private CommentRepository commentRepository;

    @Resource
    private ThumbUpService thumbUpService;


    /**
     * 排序分页方法
     *
     * @param offset
     * @param limit
     * @return
     */
    public Page<Goods> findAll(String offset, String limit) {
        return findAll(offset, limit, "", "");
    }


    /**
     * 根据用户查询
     *
     * @param offset
     * @param limit
     * @param orderby
     * @param direction
     * @param userId
     * @return
     */
    public Page<Goods> findByUserId(String offset, String limit, String orderby, String direction, String userId) {
        PageRequest pr = PageUtils.compositePage(offset, limit, orderby, direction);
        Page<Goods> page = articleRepository.findByUserId(userId, pr);
        return page;
    }

    /**
     * 排序分页方法
     *
     * @param offset
     * @param limit
     * @return
     */
    public Page<Goods> findAll(String offset, String limit, String orderby, String direction) {
        PageRequest pr = PageUtils.compositePage(offset, limit, orderby, direction);
        Page<Goods> page = articleRepository.findAll(pr);
        return page;
    }

    /**
     * 排序分页方法
     * 根据Type查询
     *
     * @param offset
     * @param limit
     * @return
     */
    public Page<Goods> findAllByType(String offset, String limit, String orderby, String direction, String type) {
        PageRequest pr = PageUtils.compositePage(offset, limit, orderby, direction);
        Page<Goods> page = articleRepository.findByTypeAndStatus(type,1, pr);
        return page;
    }

    public Goods findById(String articleId) {
        Goods article = articleRepository.findOne(articleId);
        return article;
    }

    @Transactional
    public Thumbup thumbup(Thumbup thumbup) {
        Thumbup effectRow = thumbUpService.thumbup(thumbup);
        if (null != effectRow) {
            articleRepository.thumbUp(thumbup.getGoodsId());
        }
        return effectRow;
    }

    @Transactional
    public Comment sumbitComment(Comment comment) {
        comment = commentRepository.saveAndFlush(comment);
        //给文章增加1，
        if (null != comment) {
            articleRepository.commentUp(comment.getGoodsId());
        }
        return comment;
    }

    public List<Comment> getComment(String articleId) {
        List<Comment> list = commentRepository.findByGoodsIdOrderByCreateTimeDesc(articleId);
        return list;
    }

    public List<Goods> findByUserId(String userId) {
        List<Goods> list = articleRepository.findByUserId(userId);
        return list;
    }

    @Transactional
    public Goods submitArticle(Goods article) {
        article = articleRepository.saveAndFlush(article);
        return article;
    }

    public List<Goods> findExcellents() {
        List<Goods> list = articleRepository.findExcellents();
        return list;
    }
}
